using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using BoOnce.Xml;
using BoOnce.Core.Helpers;

namespace BoOnce.Core.Input
{
    class KeyboardInput
    {

        public const int MAX_PLAYER = 4;

        public readonly KeyboardState[] CurrentKeyboardStates;
        public readonly KeyboardState[] LastKeyboardStates;

        public KeyboardXml kbConfig;

        public KeyboardInput(String keyboardConfigPath)
        {
            kbConfig = (KeyboardXml)XmlStorage.Load(keyboardConfigPath, typeof(KeyboardXml));
            CurrentKeyboardStates = new KeyboardState[MAX_PLAYER];
            LastKeyboardStates = new KeyboardState[MAX_PLAYER];
        }

        public void Update()
        {
            for (int i = 0; i < MAX_PLAYER; i++)
            {
                LastKeyboardStates[i] = CurrentKeyboardStates[i];
                CurrentKeyboardStates[i] = Keyboard.GetState((PlayerIndex)i);
            }
        }

        public bool IsNewKeyPress(PlayerIndex playerIndex, Keys key)
        {
            return (CurrentKeyboardStates[(int)playerIndex].IsKeyDown(key) &&
        LastKeyboardStates[(int)playerIndex].IsKeyUp(key));
        }

        public bool IsPress(PlayerIndex playerIndex, Keys key)
        {
            return CurrentKeyboardStates[(int)playerIndex].IsKeyDown(key);
        }
    }
}
